package com.example.talkheart

import android.os.Bundle
import android.util.Log
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.talkheart.adapter.AccountAdapter
import com.example.talkheart.adapter.TeacherAdapter
import com.example.talkheart.database.TeacherDatabaseDao
import com.example.talkheart.utils.showToast
import java.lang.Thread.sleep
import java.util.*

class MainPageActivity : AppCompatActivity() {
    private lateinit var heartAnalysis: ImageView
    private lateinit var accountManage: ImageView
    private lateinit var deviceManage: ImageView
    private lateinit var heartHelp: ImageView
    private val createButton: Button by lazy { findViewById<Button>(R.id.createButton) }
    private val userNameCreateText: EditText by lazy { findViewById<EditText>(R.id.userNameCreateText) }
    private val passwordCreateText: EditText by lazy { findViewById<EditText>(R.id.passwordCreateText) }
    private val analysisImg: ImageView by lazy { findViewById<ImageView>(R.id.analysis_img) }
    private val analysisButton: Button by lazy { findViewById<Button>(R.id.analysis_button) }
    private val analysisScore: TextView by lazy { findViewById<TextView>(R.id.analysis_score) }

    private var pageName : String = "主页面"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_page)

        bindItemsListener()
    }

    private fun bindItemsListener(){
        heartAnalysis = findViewById(R.id.heartAnalysis)
        accountManage = findViewById(R.id.accountManage)
        heartHelp = findViewById(R.id.heartHelp)
        deviceManage = findViewById(R.id.deviceManage)
        heartAnalysis.setOnClickListener {
            setContentView(R.layout.heart_analysis_page)
            val loadingImage: ImageView = findViewById(R.id.loadingImage)
            pageName = "情感分析"
            Glide.with(this)
                .load(R.drawable.loadding)
                .into(loadingImage)
            analysisButton.setOnClickListener {
                var i = true
                Thread{
                    sleep(3000)
                    i=false
                }.start()
                while (i){

                }
                renewHeartScore()
            }
        }

        accountManage.setOnClickListener {
            setContentView(R.layout.account_manage_page)
            pageName = "社交账号"
            val accountRecyclerView : RecyclerView = findViewById(R.id.accountRecyclerView)

            val linearLayoutManager  = LinearLayoutManager(this)
            accountRecyclerView.layoutManager = linearLayoutManager

            val accountAdapter = AccountAdapter()

            accountRecyclerView.adapter = accountAdapter
            createButton.setOnClickListener {
                accountAdapter.addItem(userNameCreateText.text.toString())
            }
        }
        heartHelp.setOnClickListener {
            setContentView(R.layout.heart_help_page)
            pageName = "心理辅导"

            val teacherRecyclerView : RecyclerView = findViewById(R.id.teacher_recycler_view)
            val appointButton : Button = findViewById(R.id.appointButton)

            val linearLayoutManager  = LinearLayoutManager(this)
            teacherRecyclerView.layoutManager = linearLayoutManager

            val teacherAdapter = TeacherAdapter()
            teacherRecyclerView.adapter = teacherAdapter

            //Tips:i的作用就是确保在数据库线程查询完前，一直执行UI更新
            var i = true

            Thread{
                teacherAdapter.setDataList()
                i=false
            }.start()
            //Tips:更新UI的操作只能在UI线程，而数据库操作又不能在UI线程...
            while (i)
                teacherAdapter.refresh()
            appointButton.setOnClickListener { "预约成功".showToast() }
        }

        deviceManage.setOnClickListener { setContentView(R.layout.device_manage_page)
            pageName="设备管理"}
    }

    private fun renewHeartScore(){
        val list:List<Int> = listOf(59,80,95)
        val index = Random().nextInt(3)
        analysisScore.text = list[index].toString()
        if(list[index]<30){
            analysisImg.setImageResource(R.drawable.sad)
        }else if(list[index]<70){
            analysisImg.setImageResource(R.drawable.normal)
        }else{
            analysisImg.setImageResource(R.drawable.smile)
        }
    }


    override fun onBackPressed() {
        if(!pageName.equals("主页面")){
            pageName = "主页面"
            setContentView(R.layout.main_page)
            bindItemsListener()
        }else{
            super.onBackPressed()
        }
    }
}